/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.resources
{
	import be.wellconsidered.apis.lighthouse.LightHouse;
	import be.wellconsidered.apis.lighthouse.connectors.*;
	import be.wellconsidered.apis.lighthouse.connectors.events.*;
	import be.wellconsidered.apis.lighthouse.resources.events.TicketEvents;
	import be.wellconsidered.logging.Logger;
	
	public class Tickets extends AbstractResource
	{		
		private var _xmlNewTicket:XML;
		
		public function Tickets(lighthouseReference:LightHouse)
		{
			super(lighthouseReference);
		}
		
		/**
		 * List Tickets
		 */
		public function listTickets(projectID:int):void
		{
			Logger.log("List Tickets");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/tickets.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onTicketListingComplete, false, 0, true);
			
			function onTicketListingComplete(e:GETEvents):void
			{
				dispatchEvent(new TicketEvents(TicketEvents.LIST_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Get Ticket
		 */
		public function getTicket(projectID:int, ticketID:int):void
		{
			Logger.log("Get Ticket (" + ticketID + ")");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/tickets/" + ticketID + ".xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onTicketFetchComplete, false, 0, true);
			
			function onTicketFetchComplete(e:GETEvents):void
			{
				dispatchEvent(new TicketEvents(TicketEvents.GET_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * New Ticket
		 */
		public function newTicket(projectID:int):void
		{
			Logger.log("New Ticket");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/tickets/new.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onNewTicketDataComplete, false, 0, true);
			
			function onNewTicketDataComplete(e:GETEvents):void
			{
				_xmlNewTicket = new XML(e.data);
				
				dispatchEvent(new TicketEvents(TicketEvents.NEW_COMPLETE, false, false, _xmlNewTicket));	
			}
		}
		
		/**
		 * Create Ticket
		 */
		public function createTicket(projectID:Number, ticketTitle:String, ticketBody:String, ticketState:String, ticketAssignedUserID:int = -1, ticketMilestoneID:int = -1, ticketArrayOfTags:Array = null):void
		{
			Logger.log("Create Ticket");
			
			var xmlTicket:XML = _xmlNewTicket;
			
			xmlTicket..title = ticketTitle;
			xmlTicket..body = ticketBody;
			xmlTicket..state = ticketState;
			
			if(ticketAssignedUserID > 0)
				xmlTicket..elements("assigned-user-id").nodeValue = ticketAssignedUserID;
			
			if(ticketMilestoneID > 0)
				xmlTicket..elements("milestone-id").nodeValue = ticketMilestoneID;;
			
			if(ticketArrayOfTags != null)
				xmlTicket..tag = ticketArrayOfTags.toString();
			
			trace(xmlTicket);
					
			var connPOST:POST = new POST(_lhRef.getURL() + "projects/" + projectID + "/tickets.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlTicket);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateTicketComplete, false, 0, true);
			
			function onCreateTicketComplete(e:POSTEvents):void
			{				
				dispatchEvent(new TicketEvents(TicketEvents.CREATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Update Ticket
		 */
		public function updateTicket(projectID:int, ticketXML:XML):void
		{
			Logger.log("Update Ticket (" + ticketXML.number + ")");
			
			var connPUT:PUT = new PUT(_lhRef.getURL() + "projects/" + projectID + "/tickets/" + ticketXML.number + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), ticketXML);
			
			connPUT.addEventListener(PUTEvents.COMPLETE, onUpdateTicketComplete, false, 0, true);
			
			function onUpdateTicketComplete(e:PUTEvents):void
			{				
				dispatchEvent(new TicketEvents(TicketEvents.UPDATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Delete Ticket
		 */
		public function deleteTicket(projectID:int, ticketID:int):void
		{
			Logger.log("Delete Ticket (" + ticketID + ")");
			
			var connDELETE:DELETE = new DELETE(_lhRef.getURL() + "projects/" + projectID + "/tickets/" + ticketID + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding());
			
			connDELETE.addEventListener(DELETEEvents.COMPLETE, onDeleteTicketComplete, false, 0, true);
			
			function onDeleteTicketComplete(e:DELETEEvents):void
			{				
				dispatchEvent(new TicketEvents(TicketEvents.DELETE_COMPLETE, false, false, e.data));	
			}
		}
	}
}